import { createRouter, createWebHistory,createWebHashHistory } from 'vue-router'
import Login from '../views/Login.vue'
import Main from '../views/Main.vue'
import Chat from '../views/Chat.vue'
import CustomBoard from '../views/CustomBoard.vue'
import Cut from '../views/Cut.vue'

const routes = [
  {
    path: '/', 
    redirect:'/login'
  },
  {
    path: '/login',
    component: Login
  },
  {
    path: '/main',
    component: Main
  },
  {
    path: '/chat',
    component: Chat
  },
  {
    path: '/board',
    component: CustomBoard
  },
  {
    path: '/cut',
    component: Cut
  }
]

const router = createRouter({
  linkActiveClass: 'active',
  history: createWebHashHistory(), 
  routes 
})

router.beforeEach((to, from, next) => {
    const isAuthenticated = checkAuth() // 检查用户是否已登录
    if (to.meta.requiresAuth && !isAuthenticated) {
      next('/login') // 如果未登录，重定向到登录页面
    } else {
      next()
    }
  })
  
  // 模拟检查登录状态的函数
  function checkAuth() {
    // 这里可以根据实际情况判断用户是否登录
    return localStorage.getItem('isLoggedIn') === 'true'
  }

export default router